package com.baiyun.lovePush.service.impl;

import com.baiyun.lovePush.entity.Weather;
import com.baiyun.lovePush.service.IPushService;
import com.baiyun.lovePush.utils.*;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 模板发送的实现类
 * @author 派大星
 */
@Service
public class IPushServiceImpl implements IPushService {

    @Override
    public void push()  {

        // 1，配置appid和secret
        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        //配置appid
        wxStorage.setAppId("wxf7276d068023fc");
        //配置appSecret
        wxStorage.setSecret("541fa0401ac9340e4b6c77909a8a95");
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);

        // 2.创建模板，设置接收方openid和模板id
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                //接收方微信openId
                .toUser("oPWLm6BX81fhjdB1K-qMENSL6appI")
                //模板Id
                .templateId("MHFMnKNWaw0Hy4P-boFxbAHcO4thXc8Dhwjgt")
                .build();

        // 3,配置天气信息，彩虹屁信息和恋爱天数信息
        String  loveDays = String.valueOf(DataUtils.getLoveDays("2002-3-13"));  //设置你们是从哪一天开始恋爱滴

        Weather weather = WeatherUtils.getWeather();
        templateMessage.addData(new WxMpTemplateData("riqi", weather.getDate() + "  " + weather.getWeek(), "#00BFFF"));
        templateMessage.addData(new WxMpTemplateData("city", weather.getCity(), "#e6b422"));
        templateMessage.addData(new WxMpTemplateData("tianqi", weather.getText_now(), "#165eaf"));
        templateMessage.addData(new WxMpTemplateData("low", weather.getLow() + "", "#a4c9dd"));
        templateMessage.addData(new WxMpTemplateData("high", weather.getHigh() + "", "#bf242a"));
        templateMessage.addData(new WxMpTemplateData("wind_dir", weather.getWind_dir() + "", "#FF6347"));
        templateMessage.addData(new WxMpTemplateData("wind_class", weather.getWind_class() + "", "#FF6347"));
        templateMessage.addData(new WxMpTemplateData("caihongpi", CaiHongPiUtils.getCaiHongPiTianGou().get(0), "#FF69B4"));
        templateMessage.addData(new WxMpTemplateData("lianai",loveDays, "#FF1493"));


        //备注信息,到了生日那天会自动修改成提前设置好的备注
        String beizhu = "备注信息";

        //获取当前时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String today = format.format(date);
        //根据“-”截取日期字符串，产生字符串数组
        String[] split = today.substring(0).split("-");
                            //月                   //日
        if (split[1].equals("3")&& split[2].equals("13")){      //设置对方的生日
            beizhu = "宝贝生日快乐！！！";
        }
                             //月                    //日
        if (split[1].equals("11")&& split[2].equals("11")){     //设置自己的生日
            beizhu = "今天是你宝贝的生日，嘿嘿！";
        }


        //设置距离下一个生日还有多少天
        int birthday = 0;
        int hisbirthday = 0;
        try {
            birthday = DataUtils.getBirthDays("2002-03-13");     //设置对方的生日
            hisbirthday = DataUtils.getBirthDays("2001-08-10");   //设置自己的生日
        } catch (Exception e) {
            e.printStackTrace();
        }

        templateMessage.addData(new WxMpTemplateData("beizhu", beizhu, "#FF0000"));
        templateMessage.addData(new WxMpTemplateData("hersBirthday","距离爱妃生日还有"+birthday+"天", "#b44c97"));
        templateMessage.addData(new WxMpTemplateData("hisBirthday","距离狗子生日还有"+hisbirthday+"天", "#f5b199"));


        try {
            System.out.println(templateMessage.toJson());
            System.out.println(wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage));
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            e.printStackTrace();
        }
    }


}
